﻿using AutoMapper;
using BS.Domain;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Write.Api.Handler.Photo
{
    public class AddPictureCommandHandler : IRequestHandler<InsertPrictureRequestDTO, ApiResult>
    {
        private readonly IPicturesRepository _picturesRepository;

        private readonly IMapper _mapper;
        public AddPictureCommandHandler(IPicturesRepository picturesRepository, IMapper mapper)
        {
            _picturesRepository = picturesRepository;
            _mapper = mapper;
        }
        public async Task<ApiResult> Handle(InsertPrictureRequestDTO request, CancellationToken cancellationToken)
        {
            try
            {
                var picture = _mapper.Map<Pictures>(request);
                var result= await _picturesRepository.InsertAsync(picture);
                return ApiResult.Ok(result);
            }
            catch (Exception ex)
            {
                return ApiResult.Fail(ex.Message);
            }
        }
    }
}
public class InsertPrictureRequestDTO:IRequest<ApiResult>
{
    /// <summary>
    /// 图片
    /// </summary>
    public string PictureUrl { get; set; } = string.Empty;
    /// <summary>
    /// 图片类型
    /// </summary>
    public PicEnum PicType { get; set; }
    /// <summary>
    /// 硕源ID
    /// </summary>
    public int TrId { get; set; }
}